#ifndef CONNECTION_H
#define CONNECTION_H

#include <QObject>
#include <im.h>
#include <xmpp.h>
#include <xmpp_rosteritem.h>



namespace XMPP{
    class RosterItem;
    class Jid;
}

using XMPP::RosterItem;
using XMPP::Jid;

class Connection : public QObject
{
Q_OBJECT
public:
    explicit Connection(QObject *parent, Optra::Proxy* p);
    ~Connection();

    void start();
    void stop();

    bool isConnected()const;
    bool isActive()const;
    void setUser(const QString& user);
    void setPassword(const QString& password);

signals:
    void ready();
private slots:
    void cs_warning(int warn);
    void cs_error( int err );
    void cs_authenticated();
    void cs_needAuthParams( bool user, bool passwd, bool realm);
    void cs_connected();
    void cs_securityLayerActivated(int type);
    void cs_connectionClosed();
    void cs_delayedCloseFinished();
    void cs_readyRead();
    void cs_stanzaWritten();

    void conn_srvLookup(const QString &server);
    void conn_srvResult(bool b);
    void conn_httpSyncStarted();
    void conn_httpSyncFinished();

    void tls_handshaken();

    void client_rosterItemAdded(const RosterItem &r);
    void client_rosterItemUpdated(const RosterItem &r);
    void client_rosterItemRemoved(const RosterItem &r);
    void client_rosterRequestFinished(bool success, int c, const QString &str);
    void client_subscription(const Jid &j, const QString &str, const QString& nick);
    void client_debugText(const QString &str);

    void client_xmlIncoming(const QString &s);
    void client_xmlOutgoing(const QString &s);


    void sessionStart_finished();

private:    
    void sessionStarted();
    void cleanup();
    bool active, connected;

    Jid jid_;
    XMPP::QCATLSHandler *tlsHandler;
    XMPP::AdvancedConnector* conn_;
    XMPP::ClientStream* stream_;
    XMPP::Client* client_;


    QCA::TLS *tls;
    QCA::Initializer* init_;

    QString pass_;

    Optra::Proxy* px_;
};

#endif // CONNECTION_H
